package me.jiatao.javestudy.generic.generic_array_type;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by JiaTao on 2018/9/12.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */


public class GenericArrayTypeMain<T> {
    public T[] t;
    public List<String>[] listArray;

    public static void main(String[] args) throws Exception {
        Field fieldListArray = GenericArrayTypeMain.class.getField("listArray");
        Type typeListArray = fieldListArray.getGenericType();
        if (typeListArray instanceof GenericArrayType) {
            GenericArrayType genericArrayType = (GenericArrayType) typeListArray;
            System.out.println(genericArrayType.getGenericComponentType());
        }

        Field fieldT = GenericArrayTypeMain.class.getField("t");
        Type typeT = fieldT.getGenericType();
        if (typeT instanceof GenericArrayType) {
            GenericArrayType genericArrayType = (GenericArrayType) typeT;
            System.out.println(genericArrayType.getGenericComponentType());
        }
    }

    public static void clazzGenericArrayType() {
        PointArrayImpl<Integer> impl = new PointArrayImpl<Integer>();
        Class<?> clazz = impl.getClass();

        Type[] interfaces = clazz.getGenericInterfaces();
        for (Type type : interfaces) {
            if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                Type[] actualArgs = pt.getActualTypeArguments();
                for (Type arg : actualArgs) {
                    if (arg instanceof GenericArrayType) {
                        GenericArrayType arrayType = (GenericArrayType) arg;
                        Type comType = arrayType.getGenericComponentType();
                        Class<?> typeClass = (Class) comType;
                        System.out.println("数组类型为：" + typeClass.getName());
                    }
                }
            }
        }
    }
}
